<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选择文本框</title>
</head>
<body>
    <!-- 选择文本框 -->
    <label for="username">
        姓名
    </label>
    <input type="text" id="username" value="hello world"/>
    <br>
    <button id="myBtn">选择文本框</button>
</body>
<script>
  const inputDOM = document.getElementById('username');
  inputDOM.onclick = function(event){
    inputDOM.setSelectionRange(0,inputDOM.value.length - 3)
  }
  //   inputDOM.addEventListener('select',(event)=>{
    //     console.info('文本框内所有文本:'+inputDOM.value);// hello world
    //     console.info('文本框内用户选中的文本:'+inputDOM.value.substring(inputDOM.selectionStart,inputDOM.selectionEnd));//
    //   })
</script>
</html>

